﻿using PMS.Core.Configuration.CommonEnumConfigs;
using PMS.Data.Entities.Stores;
using System;
using System.Linq;

namespace PMS.Data.Entities.Messages
{
    /// <summary>
    /// Represents a email message template
    /// </summary>
    public partial class EmailMessageTemplate : BaseEntity, IStoreMappingSupported
    {
        #region Fields
        /// <summary>
        /// 允许延迟时间可选范围：单位(分)
        /// min: 10 minutes max: 7 days
        /// </summary>
        private readonly int[] _availableMinutes = { 
            10, 30,   // 一小时以内
            60, 120, 180, 240, 300, 360, 420, 480, 540, 600,  //一天以内(1~10小时)
            720, 1440, 2160, 3600, 5040 //一周以内(一天/两天/三天/五天/七天)
        };
        private int _period;
        #endregion

        /// <summary>
        /// Gets or sets the name
        /// </summary>
        public string TemplateName { get; set; }

        /// <summary>
        /// Gets or sets the BCC Email addresses
        ///抄送地址 邮件抄送地址(原则上不超过5个)
        /// key/value配对，多个地址采用字符串拼接，使用 EmailAddress:EmailDisplayNmae;进行多组地址组合
        /// </summary>
        public string BccEmailAddresses { get; set; }

        /// <summary>
        /// Gets or sets the subject
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// Gets or sets the body
        /// </summary>
        public string Body { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the template is active
        /// Setted on BaseEntity
        /// </summary>
        //public bool IsActive { get; set; }
        //public DateTime CreatedOnTimeUtc { get; set; }
        //public DateTime UpdatedOnTimeUtc { get; set; }

        /// <summary>
        /// Gets or sets the period of message delay : Time Unit: Minute
        /// 为防止延迟时间过短没有意义，在存储延迟时间时需要使用规范时间
        /// 如： 10/30/60/120....
        /// </summary>
        public int DelayPeriodMinutes {
            get
            {
                if (_period <= 0) return 0;
                //Find Closest match in availableMinutes Numbers
                return (from n 
                        in _availableMinutes.ToList() 
                        orderby Math.Abs(n - _period) 
                        select n).First();
            }
            set => _period = value;
        }

        /// <summary>
        /// 模板邮件发送类型： 
        ///     触发类邮件： 则
        /// </summary>
        public EmailSenderType EmailSenderType { get; set; }
        /// <summary>
        /// Gets or sets the used email account identifier
        /// </summary>
        public int EmailAccountId { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the entity is limited/restricted to certain stores
        /// </summary>
        public bool LimitedToStores { get; set; }

    }
}
